package com.ddm.controller.vx;

import com.ddm.constant.MessageConstant;
import com.ddm.context.BaseContext;
import com.ddm.dto.vxDTO.QuestionAnalysis;
import com.ddm.dto.vxDTO.TestDTO;
import com.ddm.entity.vxEntity.Collection;
import com.ddm.entity.vxEntity.Question;
import com.ddm.exception.DataErrorException;
import com.ddm.result.Result;
import com.ddm.service.vxService.CollectionService;
import com.ddm.service.vxService.ExamService;
import com.ddm.service.vxService.QuetsionsService;
import com.ddm.service.vxService.TestService;
import com.ddm.vo.vxVo.QuestionVO;
import com.ddm.vo.vxVo.ScoreVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/exam")
@Slf4j
public class StartExamController {

    @Autowired
    ExamService examService;
    @Autowired
    TestService testService;
    @Autowired
    QuetsionsService quetsionsService;
    @Autowired
    CollectionService collectionService;

    @GetMapping("/start/{id}")
    @ApiOperation("开始考试")
    public Result startExam(@PathVariable Long id){
        log.info("开始考试");
        examService.startExam(id);
        return Result.success("开始考试");
    }

    @PostMapping("/submit/{id}")
    @ApiOperation("提交试卷")
    public Result<ScoreVO> submitExam(@PathVariable Long id, @RequestBody List<TestDTO> testDTOS){
        log.info("提交试卷");
        if (testDTOS == null || testDTOS.isEmpty()){
            throw new DataErrorException(MessageConstant.NULL_DATA);
        }
        return Result.success( testService.submitExam(id , testDTOS));
    }

    @GetMapping("/analysis/{id}")
    @ApiOperation("题目解析")
    public Result<List<QuestionAnalysis>> analysisQuestion(@PathVariable Long id){
        log.info("题目解析");
        if (id == null){
            throw new DataErrorException(MessageConstant.NULL_DATA);
        }
        return Result.success(examService.analysisQuestion(id));

    }

    @PostMapping("/star/{id}")
    @ApiOperation("收藏题目")
    public Result starQuestion(@PathVariable Long id){
        log.info("收藏试题");
        Question question = quetsionsService.getById(id);
        if (question == null ){
            throw  new DataErrorException(MessageConstant.ERROR_DATA);
        }
        Long userId = BaseContext.getCurrentId();
        Collection collection = Collection.builder().questionId(question.getId()).userId(userId).build();
        collectionService.save(collection);
        return Result.success("收藏成功");
    }

    @GetMapping("/nextQuestion/{id}")
    @ApiOperation("随机获取下一道题")
    public Result<QuestionVO> getNextQuestion(@PathVariable Long id ){
        log.info("随机获取下一题");
        return Result.success(examService.getNextQuetsion(id));
    }
    /**
     * 根据考试id随机获取下一道题
     */

    @GetMapping("/previousQuestion/{id}")
    @ApiOperation("获取上一题")
    public Result<QuestionVO> getPreviousQuestion(@PathVariable Long id){
        log.info("获取上一题");
        return Result.success(examService.getPreviousQuestion(id));
    }
}
